Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

510
Visualizações
Failed to load module script: Expected a JavaScript module [vite] [react] [golang]

The error which I was facing

I am using vite as build tool for my react app and golang as backend.

I built the app for production and host the app on my http server.

my directory structure:

server
  |- dist
  |    | index.html
  |    |- assets
  |         | index.js
  |         | index.css
  | main.go

To host my files the code looks like (inside main.go)

fs := http.FileServer(http.Dir("./dist"))
http.Handle("/", fs)

in index.html

<script type="module" crossorigin src="/assets/index.fd457ca0.js"></script>
<link rel="stylesheet" href="/assets/index.bdcfd918.css">

The code did actually send correct files but with wrong headers.

my network panel in dev tools

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

So I had to write my own file server to set the headers manually like:

contentTypeMap := map[string]string{
    ".html": "text/html",
    ".css":  "text/css",
    ".js":   "application/javascript",
}

filepath.Walk("./dist", func(path string, info os.FileInfo, err error) error {
    if err != nil {
        log.Fatalf(err.Error())
    }
    if info.IsDir() {
        return err
    }

    dirPath := filepath.ToSlash(filepath.Dir(path))
    contentType := contentTypeMap[filepath.Ext(info.Name())]
    handlePath := "/" + strings.Join(strings.Split(dirPath, "/")[1:], "/")

    hf := func(w http.ResponseWriter, r *http.Request) {
        w.Header().Add("Content-Type", contentType) // <---- key part
        http.ServeFile(w, r, path)
    }

    if handlePath != "/" {
        handlePath += "/" + info.Name()
    }

    mainRouter.HandleFunc(handlePath, hf)
    return nil
})

(please optimize if the code is bad, I made the solution myself and I tried so many stuff to fit my needs)

Now with that I recevied the correct files with correct headers.

And I couldn't find any solutions to work with custom headers using http.FileServer in http package. And please provide a easy solution if it exists.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda